﻿using System;
using System.Linq;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;

namespace iBeauty.App.Helper
{
    class MyReportToExcel
    {
        public Boolean ReportToExcel(string[] HeaderColum, System.Data.DataTable dt)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
                Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
                Worksheet ws = (Worksheet)xla.ActiveSheet;

                xla.Visible = true;
                for (int i = 0; i < HeaderColum.Count(); i++)
                {
                    ws.Cells[1, i + 1] = HeaderColum[i];
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        ws.Cells[2 + i, j + 1] = dt.Rows[i][j].ToString();
                    }
                return true;
            }
            catch { return false; }
        }

        public Boolean ReportToExcel(DataGridView dgv)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
                Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
                Worksheet ws = (Worksheet)xla.ActiveSheet;

                xla.Visible = true;
                for (int i = 0; i < dgv.ColumnCount; i++)
                {
                    ws.Cells[1, i + 1] = dgv.Columns[i].HeaderText.ToString();
                }
                for (int i = 0; i < dgv.RowCount; i++)
                    for (int j = 0; j < dgv.ColumnCount; j++)
                    {
                        ws.Cells[2 + i, j + 1] = dgv.Rows[i].Cells[j].Value.ToString();
                    }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
